package gonz.data.swim;

import gonz.common.PriceAndArticleParser;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
import java.util.List;

public class Bottom {
    public static final Logger log = Logger.getLogger(Bottom.class);

//    public static final String[] PRICE_ARTICLE_XPATHS = new String[]{
//            "//div[@id='related-product-1']/div/p[5]",   //
//            "//div[@id='related-product-1']/div/p[4]",
//            "//div[@id='related-product-1']/div/p[3]",     //
//            "//div[@id='related-product-1']/div/p[2]"};

    public static final String[] PRICE_XPATHS = new String[]{
            "//div[@id='content']/div/div[2]/form/div[2]/div[2]/div[2]/div[2]/div/div/div/b",
            "//div[@id='content']/div/div[2]/form/div/div[2]/div[2]/div[2]/div/div/div/b"};
    public static final String[] ARTICLE_XPATHS = new String[]{
            "//div[@id='content']/div/div[2]/form/div[2]/div[2]/div[2]/div[2]/div/div[2]",
            "//div[@id='content']/div/div[2]/form/div/div[2]/div[2]/div[2]/div/div[2]",
            //"//div[@id='content']/div/div[2]/form/div/div[2]/div/div/div[3]"
    };

    public static final String UNKNOWN_PRICES_SYMBOL = "";
    public static final String UNKNOWN_ARTICLE_SYMBOL = "";

    private WebDriver webDriver;

    private String article;
    private Swim swim;
    private List<String> prices;

    public Bottom(WebDriver webDriver, Swim swim) {
        this.webDriver = webDriver;
        this.swim = swim;
        this.prices = grabPrices();
        this.article = grabArticle();
//        if (!isPriceExists()) {
//            this.price = PriceAndArticleParser.parseAllPrices(pricesAndArticle);
//            if (this.price.equalsIgnoreCase(PriceAndArticleParser.UNKNOWN_SYMBOL))
//                log.error("Price for Bottom not found. " + webDriver.getCurrentUrl());
//        }
    }


    public String getArticle() {
        return article;
    }

    public Swim getSwim() {
        return swim;
    }

    private List<String> grabPrices() {
        List<String> result = new ArrayList<String>();
        for (int i = 0; i < PRICE_XPATHS.length; i++) {
            try {
                List<WebElement> elements = webDriver.findElements(By.xpath(PRICE_XPATHS[i]));
                if (elements == null || elements.isEmpty()) {
                    log.debug("Can't find Bottom price element: " + PRICE_XPATHS[i]);
                } else {
                    for (WebElement element : elements) {
                        if (element != null && !element.getText().isEmpty()) {
                            result.add(element.getText());
                        }
                    }
                    if (!result.isEmpty()) {
                        log.debug("Bottom prices has been grabbed: " + PRICE_XPATHS[i] + " Result:" + result);
                        return result;
                    }
                }
            } catch (NoSuchElementException e) {
                log.debug("Can't find Bottom price element: " + PRICE_XPATHS[i]);
            }

        }
        log.warn("Can't grab Bottom prices. " + webDriver.getCurrentUrl());

        result.add(UNKNOWN_PRICES_SYMBOL);
        return result;
    }

    private String grabArticle() {

        for (int i = 0; i < ARTICLE_XPATHS.length; i++) {
            try {
                WebElement element = webDriver.findElement(By.xpath(ARTICLE_XPATHS[i]));
                if (element == null || element.getText().isEmpty())
                    log.debug("Can't find Bottom article element: " + ARTICLE_XPATHS[i]);
                else {
                    if (PriceAndArticleParser.isArticleExist(element.getText())) {
                        log.debug("Bottom article has been grabbed: " + ARTICLE_XPATHS[i] +
                                " Article:" + element.getText());
                        return element.getText();
                    }
                }
            } catch (NoSuchElementException e) {
                log.debug("Can't find Bottom article element: " + ARTICLE_XPATHS[i]);
            }

        }
        log.warn("Can't grab Bottom article. " + webDriver.getCurrentUrl());
        return UNKNOWN_ARTICLE_SYMBOL;
    }

    public List<String> getPrices() {
        return prices;
    }
}
